home *** CD-ROM | disk | FTP | other *** search
- #define DO_EVENT_C
- #include "DoEvent.h"
- #undef DO_EVENT_C
-
-
- #if defined(__MWERKS__)
- #pragma segment __%Main
- #else
- #pragma segment Main
- #endif
-
-
- void
- DoEndOfGame (short winner)
- {
- Str32 empty = "\p";
- ParamText (GetPlayerName (& gSet, winner), empty, empty, empty);
-
- CurrentCursor (arrowCursor);
- switch (Alert (202, nil))
- {
- case 1:
- InitGame();
- break;
- case 2:
- DoMenuCommand ((EDIT_MENU << 16) + EDIT_UNDO);
- break;
- case 3:
- Terminate();
- break;
- }
- }
-
-
-
- void
- DoEvent (EventRecord *event)
- {
- short part;
- WindowPtr window;
- char key;
-
- switch (event->what)
- {
- case mouseDown:
- part = FindWindow (event->where, &window);
- switch (part)
- {
- case inMenuBar: // process a mouse menu command (if any)
- AdjustMenus();
- DoMenuCommand (MenuSelect (event->where));
- AdjustMenus();
- break;
- case inSysWindow: // let the system handle the mouseDown
- SystemClick (event, window);
- break;
- case inContent:
- if (window != FrontWindow())
- SelectWindow (window);
- else
- DoContentClick (window, event);
- break;
- case inDrag: // pass screenBits.bounds to get all gDevices
- DragWindow (window, event->where, &qd.screenBits.bounds);
- break;
- case inGrow: // sorry, not all screen sizes are possible.
- break;
- case inGoAway:
- if (TrackGoAway (window, event->where))
- DoMenuCommand ((FILE_MENU << 16) + FILE_QUIT);
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox (window, event->where, part))
- {
- WStateData *state = *((WStateData **)((WindowPeek) window)->dataHandle);
-
- // Since the hor/ver ratio is fixed, only some window sizes look good.
- // Currently, only two window sizes are possible,
- // so zooming in or out is alway toggling between two modes.
- // the new sizes are computed first.
-
- if (gSet.FieldWidth < kHorLarge) // now small, so make big
- {
- gSet.FieldWidth = kHorLarge;
- gSet.FieldHeight = kVerLarge;
-
- state->userState.right = state->userState.left + kHorTotal;
- state->userState.bottom = state->userState.top + kVerTotal;
- }
- else // now big, so make small
- {
- gSet.FieldWidth = kHorSmall;
- gSet.FieldHeight = kVerSmall;
-
- state->stdState.right = state->stdState.left + kHorTotal;
- state->stdState.bottom = state->stdState.top + kVerTotal;
- }
-
- // Normally, ZoomWindow would be called now,
- // but in this case SizeWindow suffices.
-
- SizeWindow (window, kHorTotal, kVerTotal, true);
- SizeItems (window);
-
- InvalBoard();
- }
- break;
- }
- break;
- case keyDown:
- case autoKey:
- // Check for menu key equivalents.
- key = event->message & charCodeMask;
- if (event->modifiers & cmdKey) // Command key down
- if (event->what == keyDown)
- {
- AdjustMenus();
- DoMenuCommand (MenuKey(key));
- AdjustMenus();
- }
- break;
- case activateEvt:
- DoActivate ((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) event->message);
- break;
- case kOSEvent:
- switch (event->message >> 24) // high byte of message
- {
- case kSuspendResumeMessage: // suspend/resume is also an activate/deactivate
-
- gInBackground = (event->message & kResumeMask) == 0;
- DoActivate (FrontWindow(), (event->message & kResumeMask) != 0);
- break;
- case mouseMovedMessage:
- AdjustCursor (& event->where);
- break;
- }
- break;
- case kHighLevelEvent:
- DoHighLevelEvent (event);
- break;
- }
- }
-
-
-
- void
- DoUpdate (WindowPtr window)
- {
- if (IsAppWindow (window) && window == gAbaloneWindow)
- UpdateBoard (window);
- }
-
-
-
- void
- DoActivate (WindowPtr window, Boolean becomingActive)
- {
- static short oldPixSize;
- short curPixSize = PixelSize();
-
- if (IsAppWindow (window))
- {
- if (becomingActive)
- {
- gBlackAndWhite = ! ColorQDAvailable() || curPixSize == 1;
-
- RestoreColors();
-
- if (curPixSize != oldPixSize && ColorQDAvailable())
- {
- Inval3D();
- AdjustMenus();
- }
- InvalBoard();
- }
- else
- {
- if (ColorQDAvailable())
- oldPixSize = curPixSize;
-
- SndDisposeChannels ();
- }
- }
- }
-
-
-
- void
- DoContentClick (WindowPtr window, EventRecord * event)
- {
- #if ! defined (__SC__) && ! defined(__MWERKS__)
- #pragma unused (window)
- #endif
-
- Point where = event->where;
-
- GlobalToLocal (& where);
-
- if (event->modifiers & shiftKey)
- DoFieldShiftClick (LocToField (where.h, where.v));
- else
- DoFieldClick (LocToField (where.h, where.v));
- }
-
-
-
- void
- DoAbout (short menu, short item)
- {
- // These define the current number of help items in the respective menus
-
- #define kAbaloneItems 3
- #define kProgramItems 7
-
- DialogPtr aboutDialog = GetNewDialog (ABOUT_ABALONE_MENU - (gSet.FieldHeight==SMALL_HEIGHT ? 1 : 0), 0, (WindowPtr) -1);
- Handle moreItems;
- short origNumItems = CountDITL (aboutDialog);
- short newNumItems;
- short itemHit;
-
- CurrentCursor (arrowCursor);
-
- do {
- // Append a DITL containing a PICT depending on chosen menu and item.
- // The DITL is inserted relative to the first item.
-
- moreItems = GetResource ('DITL', ABOUT_ABALONE_MENU + (menu == ABOUT_ABALONE_MENU ? 0 : 10) + item);
- AppendDITL (aboutDialog, moreItems, -1);
- ReleaseResource (moreItems);
- newNumItems = CountDITL (aboutDialog);
- ShowWindow (aboutDialog);
-
- do {
- ModalDialog (0, & itemHit);
- } while (! itemHit);
-
- // Remove the appended items again:
- // either a different screen (different PICT) is wanted, or we quit.
-
- ShortenDITL (aboutDialog, newNumItems - origNumItems); // back origNumItems again
-
- // Find out what menu/item to do now (or quit).
-
- switch (itemHit)
- {
- case 3: // prior item
- if (item == 1) // wrap if no prior item
- {
- if (menu == ABOUT_ABALONE_MENU)
- {
- menu += 10;
- item = kProgramItems;
- }
- else
- {
- menu -= 10;
- item = kAbaloneItems;
- }
- }
- else
- {
- item--;
- }
- break;
- case 4: // next iten
- if (item == kAbaloneItems && menu == ABOUT_ABALONE_MENU) // wrap if no next item
- {
- menu += 10;
- item =1;
- }
- else if (item == kProgramItems && menu == ABOUT_PROGRAM_MENU)
- {
- menu -= 10;
- item = 1;
- }
- else
- {
- item++;
- }
- break;
- case 2:
- DisposDialog (aboutDialog);
- }
- } while (itemHit != 2);
- }
-
-
-
- void
- DoMenuCommand (long menuResult)
- {
- short menuID = HiWord (menuResult);
- short menuItem = LoWord (menuResult);
-
- switch (menuID)
- {
- case APPLE_MENU:
-
- switch (menuItem)
- {
- case APPLE_ABOUT:
- ;
- break;
- default:
- {
- Str255 daName;
- short daRefNum;
-
- GetItem (GetMHandle (APPLE_MENU), menuItem, daName);
- daRefNum = OpenDeskAcc (daName);
- }
- break;
- }
- break;
-
- case ABOUT_ABALONE_MENU:
- case ABOUT_PROGRAM_MENU:
- {
- DoAbout (menuID, menuItem);
- }
- break;
-
- case FILE_MENU:
-
- gInterrupted |= (menuItem == FILE_NEW || menuItem == FILE_OPEN || menuItem == FILE_QUIT);
-
- switch (menuItem)
- {
- case FILE_NEW:
- ConfirmAndSaveAndMaybeThen (& InitGame);
- break;
-
- case FILE_QUIT:
- ConfirmAndSaveAndMaybeThen (& Terminate);
- break;
-
- case FILE_OPEN:
- ConfirmAndSaveAndMaybeThen (& OpenGame);
- break;
-
- case FILE_SAVE:
- SaveGame();
- break;
-
- case FILE_SAVE_AS:
- SaveGameAs();
- break;
- }
- break;
-
- case EDIT_MENU:
-
- if (SystemEdit (menuItem-1))
- break;
-
- switch (menuItem)
- {
- case EDIT_UNDO:
- UndoLastMove();
- break;
-
- default:
- break;
- }
- break;
-
- case SETTINGS_MENU:
- switch (menuItem)
- {
- default:
- DoSettingsMenu (menuItem);
- break;
-
- case SETTINGS_2_PLAYERS:
- case SETTINGS_3_PLAYERS:
- {
- short players = menuItem - SETTINGS_2_PLAYERS + 2;
- if (gFileSaved)
- {
- gTheGame.Players = players;
- gSet.Players = players;
- Inval3D(); // number of players changes, so the palette must too
- InitGame();
- }
- else switch (AskSave())
- {
- case save: // save first and fall through
- DoMenuCommand ((FILE_MENU << 16) + FILE_SAVE);
- case dont_save:
- gTheGame.Players = players;
- gSet.Players = players;
- Inval3D(); // number of players changes, so the palette must too
- InitGame();
- break;
- default:
- ;
- }
- break;
- }
- }
-
- break;
-
- case OPTION_MENU:
-
- switch (menuItem)
- {
-
- case OPTION_SOUND:
- gSet.SoundOn = ! gSet.SoundOn;
- break;
-
- case OPTION_3D_BALLS:
- gSet.Balls3D = ! gSet.Balls3D;
- Inval3D(); // palette must be allocated or disposed
- InvalBoard();
- break;
-
- case OPTION_NOTATION:
- gSet.ShowNotation = ! gSet.ShowNotation;
- InvalItems (gAbaloneWindow);
- break;
- }
- break;
-
- case COLOR_MENU:
- DoColorMenu (menuItem);
- break;
-
- case BACKGROUND_MENU:
- DoBackgroundMenu (menuItem);
- break;
-
- #ifdef NETDEBUG
- case NETTEST_MENU:
- DoNetTestMenu (menuItem);
- break;
- #endif
- }
- HiliteMenu(0);
- }
-
-
-
- void
- DoColorMenu (short colorItem)
- {
- RGBColor selectColor;
- Point loc = {-1, -1}; // this centers the dialog automagically
-
- if (! ColorQDAvailable())
- return;
-
- switch (colorItem)
- {
- case COLOR_WHITE:
- case COLOR_BLACK:
- case COLOR_GREEN:
-
- if (GetColor ( loc,
- "\pSelect a color for this player",
- & gSet.Color[colorItem],
- & selectColor
- )
- )
- gSet.Color[colorItem] = selectColor;
- break;
- case COLOR_BACKGROUND:
-
- if (GetColor ( loc,
- "\pSelect a background color",
- & gSet.Color[0],
- & selectColor
- )
- )
- {
- gSet.Color[0] = selectColor;
- RGBBackColor (& gSet.Color[0]);
- PutSettings(); // to update the dctb
- }
- break;
- }
- Inval3D(); // Palette must be recomputed if a color has changed
- InvalBoard();
- }
-
-
-
- void
- DoBackgroundMenu (short item)
- {
- DialogPtr pictDialog = GetNewDialog (rBackgroundSelect, 0, (WindowPtr) -1);
- Handle moreItems;
- short origNumItems = CountDITL (pictDialog);
- short itemHit;
-
- Assert (item == 1, INTERNAL_ERROR);
- Assert (gSet.Balls3D, INTERNAL_ERROR);
-
- CurrentCursor (arrowCursor);
-
- // Showing all the PICT's we have for the background costs a lot of memory.
- // We can free a lot by very temporarily switching to simple graphics;
- // it doesn't really show yet, and the background is bound to change anyhow.
-
- gSet.Balls3D = false; // just a while
- Inval3D(); // free up all background picture stuff
- InvalBoard();
-
- // Append one or more DITL's containing pictures useable for the background.
-
- moreItems = GetResource ('DITL', rBackgroundSelectPict);
- AppendDITL (pictDialog, moreItems, -1);
- ReleaseResource (moreItems);
- // newNumItems = CountDITL (pictDialog);
- ShowWindow (pictDialog);
-
- do {
- ModalDialog (0, & itemHit);
- } while (! itemHit);
-
- DisposDialog (pictDialog);
-
- // Now make the picture selected the background picture
-
- gSet.BackgroundPictID = 200 + itemHit - 2;
- gSet.Balls3D = true; // switch back on
- Inval3D();
- }
-
-
-
- pascal void
- PlayerBallItemProc (WindowPtr wp, short item)
- {
- short type;
- Handle colorControl;
- Rect box;
-
- GetDItem (wp, item, & type, & colorControl, & box);
-
- switch ((short) GetWRefCon (wp))
- {
- case 1:
- PaintOval (& box);;
- break;
- case 2:
- FrameOval (& box);
- break;
- case 3:
- PenPat ((ConstPatternParam) (& qd.gray));
- PaintOval (& box);
- PenNormal();
- break;
- }
- FrameOval (& box);
- }
-
-
-
- void
- DoSettingsMenu (short playerItem)
- {
- DialogPtr playerDialog;
- short item, type;
- Handle colorControl, playerKindControl, strategyControl, levelControl, nameItem;
- Rect box;
-
- // Create the strategy control on-the-fly.
-
- for (item = 0; gStrategies[item].JudgeProc.boardProc != NULL; item++)
- {
- AppendMenu (GetMenu (STRATEGY_MENU), (StringPtr) gStrategies[item].Name);
- }
-
- playerDialog = GetNewDialog (200, nil, (WindowPtr) -1);
-
-
- // Prepare all items to show their right settings, depending on global vars.
- // Show dialog when done.
-
- GetDItem (playerDialog, 3, & type, & colorControl, & box);
- SetDItem (playerDialog, 3, type, (Handle) (NewUserItemProc (PlayerBallItemProc)), & box);
-
- GetDItem (playerDialog, 4, & type, & playerKindControl, & box);
- GetDItem (playerDialog, 5, & type, & strategyControl, & box);
- GetDItem (playerDialog, 6, & type, & levelControl, & box);
- GetDItem (playerDialog, 8, & type, & nameItem, & box);
-
- SetCtlValue ((ControlHandle) playerKindControl, gSet.PlayerKind[playerItem]);
- SetCtlValue ((ControlHandle) strategyControl, gSet.Strategy[playerItem] + 1); // was +3
- SetCtlValue ((ControlHandle) levelControl, gSet.Level[playerItem] + 1);
- SetIText (nameItem, GetPlayerName (& gSet, playerItem));
-
- // All neat. Show it now.
- ShowWindow (playerDialog);
-
- // Use the refcon to pass the player item to the item proc
- // (can't add a parameter to this pascal funtion)
-
- SetWRefCon (playerDialog, (long) playerItem);
-
- for (item = 6;;)
- {
- switch (item)
- {
- case 0: // nothing
- default: // don't know
- break;
- case 1: // First do all settings stuff, then quit by falling through.
- gInterrupted = true;
- SetPlayerKind (& gSet, playerItem, GetCtlValue ((ControlHandle) playerKindControl));
- gSet.Strategy[playerItem] = GetCtlValue ((ControlHandle) strategyControl) - 1; // was -3
- {
- Str255 name;
-
- GetIText (nameItem, name);
- SetPlayerName (& gSet, playerItem, name);
- }
- gSet.Level[playerItem] = GetCtlValue ((ControlHandle) levelControl) - 1;
- if (GetCtlValue ((ControlHandle) playerKindControl) == networkPlayer)
- {
- if (Connected())
- {
- ReSyncProtocol();
- }
- else
- {
- CurrentCursor (arrowCursor);
- switch (Alert (rConnection, nil))
- {
- case 1: // Establish Connection
- ConnectionProtocol();
- break;
-
- case 2: // Wait for other
- ;
- break;
-
- case 3: // Cancel net play
- SetPlayerKind (& gSet, playerItem, macPlayer);
- break;
- }
- }
- }
- case 2: // just quit
- DisposDialog (playerDialog), playerDialog = 0;
- SetPort (FrontWindow());
- return;
-
- case 3:
- DoColorMenu (playerItem);
- DisposDialog (playerDialog), playerDialog = 0;
- SetPort (FrontWindow());
- return;
-
- case 4:
- case 5:
- case 6:
- {
- short playerKind = GetCtlValue ((ControlHandle) playerKindControl);
- Boolean isMacKind = playerKind == macPlayer;
-
- HiliteControl ((ControlHandle) strategyControl, isMacKind ? 0 : 255);
- HiliteControl ((ControlHandle) levelControl, isMacKind ? 0 : 255);
-
- if (! ConfirmPlayerKind (playerItem, playerKind))
- {
- // User cancelled in broken connection dialog.
- // Player kind remains network player.
- SetCtlValue ((ControlHandle) playerKindControl, networkPlayer);
- }
-
- SelIText (playerDialog, 8, 0, 255);
- }
- break;
- }
- ModalDialog (nil, & item);
- }
- }
-
-
-
- Boolean
- DoCloseWindow (WindowPtr window)
- {
- if (IsDAWindow (window))
- CloseDeskAcc (((WindowPeek) window)->windowKind);
- else if (IsAppWindow (window))
- CloseWindow (window);
-
- return true;
- }
-
-
-
- void
- Terminate (void)
- {
- gQuitFlag = true;
- HideWindow (gAbaloneWindow);
- PutSettings();
- }
-
-
-
- enum _answer
- AskSave (void)
- {
- if (Connected())
- {
- if (ConfirmNetQuit())
- return dont_save;
- else
- return cancel_it;
- }
- else
- {
- CurrentCursor (arrowCursor);
- return (enum _answer) CautionAlert (201, NULL);
- }
- }
-
-
-
- void
- ConfirmAndSaveAndMaybeThen (void (*doMe)(void))
- {
- if (gFileSaved && ! Connected())
- (*doMe)();
- else switch (AskSave())
- {
- case save: // Save first and then fall through.
- DoMenuCommand ((FILE_MENU << 16) + FILE_SAVE);
- case dont_save:
- (*doMe)();
- break;
- default:
- ;
- }
- }
-